// pages/teacher-list/teacher-list.js
const _function = require("../../utils/functionData.js")
const { formatTime, formatData } = require("../../utils/util")
Page({
  data: {
    teachers: [
      //   {
      //   id: 1,
      //   name: '王教授',
      //   title: '计算机科学教授',
      //   avatar: '/images/avatar1.jpg',
      //   rating: 4.8,
      //   major: '人工智能/机器学习',
      //   availableTime: '每周三 14:00-16:00',
      //   available: true
      // }, {
      //   id: 2,
      //   name: '李副教授',
      //   title: '软件工程副教授',
      //   avatar: '/images/avatar2.jpg',
      //   rating: 4.5,
      //   major: '软件开发/系统架构',
      //   availableTime: '每周五 10:00-12:00',
      //   available: false
      // }
    ],
    minHour: 10,
    maxHour: 20,
    minDate: new Date().getTime(),
    maxDate: new Date(2099, 10, 1).getTime(),
    currentDate: new Date().getTime(),
    isAvailable: false,
    teacherId: null,
    _UrlGet_: wx.getStorageSync('img_url'),
    role: 0,
    searchValue: '',
  },
  onLoad(options) {
    const searchValue = options.searchValue
    console.log('optiions', options.searchValue);
    this.setData({
      searchValue: searchValue
    })
    this.getTeacherList()
  },
  onShow() {
    this.setData({
      role: getApp().globalData.userInfo.role
    })
  },
  getTeacherList() {
    let params = {}
    if (this.data.role == 0) {
      params = {
        name: this.data.searchValue,
        studentId: getApp().globalData.userInfo.userId
      }
    } else {
      params = {
        name: this.data.searchValue
      }
    }

    _function.getTeacherList(params, this.getTeacherListBack, this)
  },
  getTeacherListBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      this.setData({
        teachers: dataAll.rows
      })
      // wx.showToast({
      //   title: dataAll.msg,
      //   icon: 'none', // 不显示图标
      //   duration: 1500, // 提示的延迟时间，单位毫秒，默认为 1500
      //   mask: true, // 是否显示透明蒙层，防止触摸穿透，默认为 `false`
      //   success: () => {
      //     // console.log('登录失败提示已显示');
      //   }
      // });
    }
  },
  onInput(event) {
    console.log(event);
    this.setData({
      currentDate: event.detail
    });

  },
  // 处理预约
  handleReservation(e) {
    console.log(123, e);
    this.setData({
      isAvailable: true,
      teacherId: e.currentTarget.dataset.id
    })
    // const teacherId = e.currentTarget.dataset.id
    // const teacher = this.data.teachers.find(t => t.id === teacherId)
    // studentId 学生ID 
    // teacherId 教师ID 
    // bookingTime 预约时间
    // if (!teacher.available) return

    // wx.navigateTo({
    //   url: `/pages/reservation/reservation?id=${teacherId}`
    // })

  },
  postBooking(e) {
    console.log('选择时间', getApp().globalData.userInfo);
    const param = {
      teacherId: this.data.teacherId,
      studentId: getApp().globalData.userInfo.userId,
      bookingTime: formatData(this.data.currentDate)// 使用 formatTime 函数格式化时间
    }
    _function.postBooking(param, this.postBookingBack, this)
  },
  postBookingBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      // console.log('预约成功');
      this.setData({
        isAvailable: false
      })
      this.getTeacherList()
      wx.showToast({
        title: dataAll.msg,
        icon: 'none', // 不显示图标
        duration: 1500, // 提示的延迟时间，单位毫秒，默认为 1500
        mask: true, // 是否显示透明蒙层，防止触摸穿透，默认为 `false`
        success: () => {
          // console.log('登录失败提示已显示');
        }
      });
    }
  },
  // 取消事件处理函数
  onCancel() {
    this.setData({
      isAvailable: false // 隐藏组件
    });
  }
})